package com.fzrskj.cloud.free.user.dao;

import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.core.utils.CloudDbDrvUtils;
import com.fzrskj.cloud.free.user.bean.UserLog;
import com.winiis.component.common.utils.other.XSSFilterUtils;
import ms.core.consts.SysErr;
import ms.core.db.IDbDrv;
import ms.core.object.CObject;
import ms.core.tool.StrTool;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserLogDao extends CObject{
	//private declarations
	private IDbDrv db=null;

	//constructor function to initialize database access object
	public UserLogDao(){
		db= CloudDbDrvUtils.getDbUtilsDrv();
	}

	//to free resource
	@Override
	public void dispose(){
		db.closeConnection();
	}

	/**
	 * 根据ID查询操作日志详情
	 *
	 * @param id
	 * @return
	 */
	public UserLog queryLogDetails(long id) {
		String sql = "select * from user_log where id=? and agentId=?";
		UserLog queryLogDetails = db.queryForBean(sql, UserLog.class, new Object[]{id, CloudContextUtils.getAgentId()});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return queryLogDetails;
	}

	/**
	 * 根据条件查询操作日志
	 *
	 * @param userName
	 * @param startTime
	 * @param endTime
	 * @param page
	 * @param pageSize
	 * @return
	 */
	public List<UserLog> queryuserLog(String userName, String startTime, String endTime, int page, int pageSize, String module, String content) {
		String sql = "select * from user_log where 1=1 and agentId=?";
		List<Object> list = new ArrayList<>();
		list.add(CloudContextUtils.getAgentId());
		if (!StrTool.isBlankStr(userName)) {
			sql += " and userName=?";
			list.add(userName);
		}
		if (!StrTool.isBlankStr(startTime)) {
			sql += "  and '" + startTime + "' <=" + "logTime";
		}
		if (!StrTool.isBlankStr(endTime)) {
			sql += " and logTime <='" + endTime + "'";
		}
		if (!StrTool.isBlankStr(module)) {
			sql += " and module=?";
			list.add(module);
		}
		if (!StrTool.isBlankStr(content)) {
			sql += " and LOCATE(?,logDesc)>0 ";
			list.add(content);
		}
		if (page > 0 && pageSize > 0) {
			sql += " order by logTime desc";
			sql += " limit " + (page - 1) * pageSize + "," + pageSize;
		}
		Object[] params = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			params[i] = list.get(i);
		}
		List<UserLog> queryuserLog = db.queryForBeans(sql, UserLog.class, params);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return queryuserLog;
	}

	/**
	 * 根据条件删除操作日志
	 *
	 * @param userName
	 * @param startTime
	 * @param endTime
	 * @param module
	 * @return
	 */
	public int deluserLog(String userName, String startTime, String endTime, String module) {
		String sql = "delete from user_log where 1=1  and agentId=?";
		List<Object> list = new ArrayList<>();
		list.add(CloudContextUtils.getAgentId());
		if (!StrTool.isBlankStr(userName)) {
			sql += " and userName=?";
			list.add(userName);
		}
		if (!StrTool.isBlankStr(startTime)) {
			sql += "  and '" + startTime + " 00:00:00' <=" + "logTime";
		}
		if (!StrTool.isBlankStr(endTime)) {
			sql += " and logTime <='" + endTime + " 23:59:59' ";
		}
		if (!StrTool.isBlankStr(module)) {
			sql += " and module=?";
			list.add(module);
		}
		Object[] params = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			params[i] = list.get(i);
		}
		int ret = db.executeSql(sql, params);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}
	/**
	 * 根据条件删除系统日志
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public int delSysLog(String startTime,String endTime){
		String sql="delete from sys_runtimelog where 1=1  and agentId=?";
		List<Object> list = new ArrayList<>();
		list.add(CloudContextUtils.getAgentId());
		if(!StrTool.isBlankStr(startTime)){
			sql +="  and '"+ startTime+" 00:00:00' <="+"logTime";
		}
		if(!StrTool.isBlankStr(endTime)){
			sql +=" and logTime <='"+ endTime+" 23:59:59'";
		}
		Object[] params = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			params[i] = list.get(i);
		}
		int ret = db.executeSql(sql, params);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	/**
	 * 根据条件查询总数
	 *
	 * @param userName
	 * @param startTime
	 * @param endTime
	 * @param module
	 * @return
	 */
	public int queryuserLogCount(String userName, String startTime, String endTime, String module, String content) {
		String sql = "select count(id) from user_log where 1=1 and agentId=?";
		List<Object> list = new ArrayList<>();
		list.add(CloudContextUtils.getAgentId());
		if (!StrTool.isBlankStr(userName)) {
			sql += " and userName=?";
			list.add(userName);
		}
		if (!StrTool.isBlankStr(module)) {
			sql += " and module=?";
			list.add(module);
		}
		if (!StrTool.isBlankStr(content)) {
			sql += " and LOCATE(?,logDesc)>0";
			list.add(content);
		}
		if (!StrTool.isBlankStr(startTime)) {
			sql += "  and '" + startTime + "' <=" + "logTime";
		}
		if (!StrTool.isBlankStr(endTime)) {
			sql += " and logTime <='" + endTime + "'";
		}
		Object[] params = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			params[i] = list.get(i);
		}
		long counts = (Long) db.queryFieldValue(sql, Long.class, params, 1);
		int count = (int) counts;
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return count;
	}

	public int addUserLog(String moduleName, int logTypeId, String logTypeName, String logDesc, int uid, String uName) {
		return this.addUserLog(moduleName, logTypeId, logTypeName, logDesc, uid, uName, CloudContextUtils.getAgentId());
	}

	/**
	 * 添加操作日志
	 *
	 * @param moduleName
	 * @param logTypeId
	 * @param logTypeName
	 * @param logDesc
	 * @param uid
	 * @param uName
	 * @return
	 */
	public int addUserLog(String moduleName, int logTypeId, String logTypeName, String logDesc, int uid, String uName, Integer agentId) {
		//XSS防注入处理
		logDesc = XSSFilterUtils.filter(logDesc);
		String sql = "insert into user_log(logDesc,logTime,logTypeId,logTypeName,module," +
				"userId,userName,agentId)" +
				"values(?,?,?,?,?," +
				"?,?,?);";
		Object[] prm = {logDesc, new Date(), logTypeId, logTypeName, moduleName,
				uid, uName, null == CloudContextUtils.getAgentId() ? CloudContextUtils.DEF_AGENT_ID : agentId};
		int ret = db.executeSql(sql, prm);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

} 